//
//  DZSpeedReportController.m
//  dzyc
//
//  Created by 吴斌 on 2024/6/17.
//

#import "DZSpeedReportController.h"
#import "DZMainController.h"
#import "DZSpeedReportCell.h"
#import "DzSpeedReportModel.h"
#import "MapKit/MapKit.h"
@interface DZSpeedReportController ()<UITableViewDelegate,UITableViewDataSource,MKMapViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *listBtn;
@property (weak, nonatomic) IBOutlet UIButton *mapBtn;

@property (weak, nonatomic) IBOutlet UIView *listView;

@property (weak, nonatomic) IBOutlet UIView *listHeadView;
@property (weak, nonatomic) IBOutlet UITextField *listTextField;
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (weak, nonatomic) IBOutlet UIButton *speedReportBtn;


@property (weak, nonatomic) IBOutlet UIView *mapView;
@property (weak, nonatomic) IBOutlet MKMapView *mapPointView;
@property (nonatomic,strong) DzSpeedReportShuJuModel *speedReportShuJuMdl;
@property (nonatomic,assign) CLLocationCoordinate2D locationCoordinate;
@property (nonatomic,strong) MKPointAnnotation *annotation;

@property (weak, nonatomic) IBOutlet UILabel *mapNameLab;
@property (weak, nonatomic) IBOutlet UILabel *mapTimeLab;
@property (weak, nonatomic) IBOutlet UIView *mapFirstView;
@property (weak, nonatomic) IBOutlet UILabel *mapWeiDuLab;
@property (weak, nonatomic) IBOutlet UILabel *mapDeepLab;

@property (weak, nonatomic) IBOutlet UIView *mapSecondView;
@property (weak, nonatomic) IBOutlet UILabel *mapJinDuLab;
@property (weak, nonatomic) IBOutlet UILabel *mapZhenJiLab;
@property (weak, nonatomic) IBOutlet UILabel *mapLocalLab;
@property (nonatomic,strong) UILabel * pinLab;

@property (nonatomic,strong) DzSpeedReportModel* speedReportMdl;
@property (nonatomic,strong) NSArray *shuju;
@end

@implementation DZSpeedReportController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    self.listTextField.delegate = self;
    self.listTextField.returnKeyType = UIReturnKeySearch;
    
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    self.listTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.listHeadView.layer.cornerRadius = DZRatio(10);
    self.listHeadView.clipsToBounds = YES;
    [self.mapBtn setTitleColor:DZHexA(@"#FFFFFF", 0.6) forState:UIControlStateNormal];
    [self makeSpeedReportList];
    // 设置地图显示区域和缩放级别
    MKCoordinateRegion region = self.mapPointView.region;
    region.span.latitudeDelta = 10; // 控制纬度范围
    region.span.longitudeDelta = 10; // 控制经度范围
    [self.mapPointView setRegion:region animated:YES];
    
    self.mapPointView.delegate = self;
    self.mapFirstView.layer.cornerRadius = DZRatio(10);
    self.mapFirstView.clipsToBounds = YES;
    self.mapFirstView.layer.borderWidth = DZRatio(1);
    self.mapFirstView.layer.borderColor = DZHexA(@"#000000", 0.26).CGColor;
    
    self.mapSecondView.layer.cornerRadius = DZRatio(10);
    self.mapSecondView.clipsToBounds = YES;
    self.mapSecondView.layer.borderWidth = DZRatio(1);
    self.mapSecondView.layer.borderColor = DZHexA(@"#000000", 0.26).CGColor;
}
- (void)makeSpeedReportList{
    DZWeakSelf
    [[DzNetHttp defaultRequest] post:DZDiZhenUrl param:@{} success:^(id  _Nonnull data) {
        NSString *dataJson = data[@"data"];
        dataJson = [dataJson stringByReplacingOccurrencesOfString:@"({" withString:@"{"];
        dataJson = [dataJson stringByReplacingOccurrencesOfString:@"})" withString:@"}"];
        NSDictionary * dataDic = [NSString dicToJsonString:dataJson];
        weakSelf.speedReportMdl = [DzSpeedReportModel mj_objectWithKeyValues:dataDic];
        weakSelf.shuju = weakSelf.speedReportMdl.shuju;
        weakSelf.speedReportShuJuMdl = weakSelf.shuju.firstObject;
        [weakSelf.listTableView reloadData];
    } fail:^(NSError * _Nonnull error) {
            
    }];
}
- (void)updataMapPoint{
    if(self.speedReportShuJuMdl){
        // 将标注添加到地图上
        [self.mapPointView removeAnnotation:self.annotation];
        
        self.locationCoordinate = CLLocationCoordinate2DMake([self.speedReportShuJuMdl.EPI_LAT doubleValue],[self.speedReportShuJuMdl.EPI_LON doubleValue]);
        self.annotation = [[MKPointAnnotation alloc] init];
        self.annotation.coordinate = self.locationCoordinate;
        // 将标注添加到地图上
        [self.mapPointView addAnnotation:self.annotation];
        //设置地图中心点位置
        [self.mapPointView setCenterCoordinate:self.locationCoordinate animated:YES];
        
        //弹框显示
        self.mapNameLab.text = self.speedReportShuJuMdl.LOCATION_C;
        self.mapTimeLab.text = self.speedReportShuJuMdl.SAVE_TIME;
        self.mapWeiDuLab.text = [NSString stringWithFormat:@"纬度:%@",self.speedReportShuJuMdl.EPI_LAT];
        self.mapDeepLab.text = [NSString stringWithFormat:@"深度:%@km",self.speedReportShuJuMdl.EPI_DEPTH];
        self.mapJinDuLab.text = [NSString stringWithFormat:@"经度:%@km",self.speedReportShuJuMdl.EPI_LON];
        self.mapZhenJiLab.text = [NSString stringWithFormat:@"震度:Ms%@",self.speedReportShuJuMdl.M];
    }
}
- (IBAction)changeList:(UIButton *)sender {
    [self.listBtn setTitleColor:DZHex(@"#FFFFFF") forState:UIControlStateNormal];
    [self.mapBtn setTitleColor:DZHexA(@"#FFFFFF", 0.6) forState:UIControlStateNormal];
    self.listView.hidden = false;
    self.mapView.hidden = true;
}
- (IBAction)changeMap:(UIButton *)sender {
    [self.listBtn setTitleColor:DZHexA(@"#FFFFFF", 0.6) forState:UIControlStateNormal];
    [self.mapBtn setTitleColor:DZHex(@"#FFFFFF") forState:UIControlStateNormal];
    self.listView.hidden = true;
    self.mapView.hidden = false;
    [self updataMapPoint];
}
- (IBAction)navBack:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)mainSet:(UIButton *)sender {
    DZMainController * mainCtrl = [[DZMainController alloc] init];
    [self.navigationController pushViewController:mainCtrl animated:YES];
}
- (IBAction)speedReport:(UIButton *)sender {
    [self.view endEditing:YES];
}
#pragma mark -- UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shuju.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return DZRatio(212);
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DZSpeedReportCell * speedReportCell = [DZSpeedReportCell showSpeedReportCell:tableView];
    speedReportCell.speedReportShuJuMdl = self.shuju[indexPath.row];
    return speedReportCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.speedReportShuJuMdl = self.shuju[indexPath.row];
    [self changeMap:self.mapBtn];
}
// 自定义标注视图
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;
    }
    static NSString *identifier = @"AnnotationIdentifier";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    
    if (pinView == nil) {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        pinView.canShowCallout = YES;
    } else {
        pinView.annotation = annotation;
    }
    pinView.image = DZImageNamed(@"dz_MapPoint");
    [pinView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(DZRatio(260)));
        make.height.equalTo(@(DZRatio(260)));
        make.center.equalTo(pinView);
    }];
    if(!self.pinLab){
        self.pinLab = [[UILabel alloc] init];
        self.pinLab.font = [UIFont systemFontOfSize:DZRatio(28) weight:UIFontWeightSemibold];
        self.pinLab.textColor = DZHex(@"#FFFFFF");
        self.pinLab.textAlignment = NSTextAlignmentCenter;
        [pinView addSubview:self.pinLab];
        [self.pinLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(@(DZRatio(0)));
        }];
    }
    self.pinLab.text = self.speedReportShuJuMdl.M;
    return pinView;
}
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    self.speedReportBtn.hidden = false;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.speedReportBtn.hidden = true;
}
// 点击键盘的搜索按钮时会来到该方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length > 0) {
        NSMutableArray *searchs = [[NSMutableArray alloc] init];
        for (DzSpeedReportShuJuModel *searchMdl in self.speedReportMdl.shuju) {
            if([searchMdl.LOCATION_C containsString:textField.text]){
                [searchs addObject:searchMdl];
            }
        }
        self.shuju = searchs.copy;
        [self.listTableView reloadData];
    }else{
        self.shuju = self.speedReportMdl.shuju;
        [self.listTableView reloadData];
    }
    [self.view endEditing:YES];
    return YES;
}
@end
